﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Taobao.Tools2
{
    #region Tool Configuration

    public class ToolConfiguration
    {
        private static ToolConfiguration current = null;
        public static ToolConfiguration Current
        {
            get {
                if (current == null)
                    Load();
                return current;
            }
        }

        public bool PreparingOperation { get; set; }
        public bool SentBeforeAllAction { get; set; }
        public bool GetRefunds { get; set; }
        public bool GetTrades { get; set; }
        public bool GetTradesInfo { get; set; }
        public bool UpdateTradesInfo { get; set; }
        public bool Sent { get; set; }
        public bool WaitBuyerPay { get; set; }
        public bool TradeNoCreatePay { get; set; }
        public bool WaitSellerSendGoods { get; set; }
        public bool EnableUpdateTradeDetail { get; set; }

        public bool UseIncrement { get; set; }
        public bool UpdateWhenGet { get; set; } 

        public int ProcessInterval { get; set; } //Minutes
        public int UpdateBeforeDays { get; set; }

        protected ToolConfiguration()
        {
            this.GetRefunds = true;
            this.GetTrades = true;
            this.GetTradesInfo = true;
            this.PreparingOperation = true;
            this.Sent = true;
            this.SentBeforeAllAction = true;
            this.UpdateTradesInfo = true;
            this.WaitBuyerPay = true;
            this.WaitSellerSendGoods = true;
            this.TradeNoCreatePay = true;
            ProcessInterval = 10;
            UpdateBeforeDays = 15;
            UpdateWhenGet = false;
            UseIncrement = false;
            EnableUpdateTradeDetail = false;
        }

        public static void Load()
        {
            var xDocument = XDocument.Load("configuration.xml");
            var processElement = xDocument.Root.Element("process");
            var configuration = new ToolConfiguration();
            configuration.PreparingOperation = GetProcessEnableAttribute(processElement, "preparingOperation");
            configuration.SentBeforeAllAction = GetProcessEnableAttribute(processElement, "sentBeforeAllAction");
            configuration.GetRefunds = GetProcessEnableAttribute(processElement, "getRefunds");
            configuration.GetTrades = GetProcessEnableAttribute(processElement, "getTrades");
            configuration.GetTradesInfo = GetProcessEnableAttribute(processElement, "getTradesInfo");
            configuration.UpdateTradesInfo = GetProcessEnableAttribute(processElement, "updateTradesInfo");
            configuration.Sent = GetProcessEnableAttribute(processElement, "sent");
            configuration.TradeNoCreatePay = GetProcessEnableAttribute(processElement, "tradeNoCreatePay");
            configuration.WaitBuyerPay = GetProcessEnableAttribute(processElement, "waitBuyerPay");
            configuration.WaitSellerSendGoods = GetProcessEnableAttribute(processElement, "waitSellerSendGoods");

            configuration.ProcessInterval = GetElementValue<int>(processElement, "processinterval", 10);
            configuration.UpdateBeforeDays = GetElementValue<int>(processElement, "updatebeforedays", 15);
            configuration.UpdateWhenGet = GetElementValue<bool>(processElement, "updateWhenGetTrade", false);
            configuration.UseIncrement = GetElementValue<bool>(processElement, "useIncrement", false);
            configuration.EnableUpdateTradeDetail = GetElementValue<bool>(processElement, "updateTradeDetail", false);
            current = configuration;
        }

        private static bool GetProcessEnableAttribute(XElement processElement, XName childElementName)
        {
            var childElement = processElement.Element(childElementName);
            return childElementName != null && childElement.Attribute("enable") != null && childElement.Attribute("enable").Value == "true";
        }
        private static void SetProcessEnableAttribtue(XElement processElement, XName childElementName, bool enabled)
        {
            var childElement = processElement.Element(childElementName);
            if (childElement == null)
            {
                childElement = new XElement(childElementName);
                processElement.Add(childElement);
            }
            var enableAttribute = childElement.Attribute("enable");
            if (enableAttribute == null)
            {
                enableAttribute = new XAttribute("enable", "true");
                childElement.Add(enableAttribute);
            }
            enableAttribute.Value = enabled ? "true" : "false";
        }

        private static T GetElementValue<T>(XElement element, XName childElementName, T defaultValue)
        {
            var childElement = element.Element(childElementName);
            return childElement != null ? Taobao.Common.Util.ChangeType<T>(childElement.Value, defaultValue) : defaultValue;
        }
        private static void SetElementValue<T>(XElement element, XName childElementName, T value)
        {
            var childElement = element.Element(childElementName);
            if (childElement == null)
            {
                childElement = new XElement(childElementName);
                element.Add(childElement);
            }
            childElement.Value = value == null ? string.Empty : value.ToString();
        }

        public static void Save(ToolConfiguration configuration)
        {
            var xDocument = XDocument.Load("configuration.xml");
            var processElement = xDocument.Root.Element("process");
            SetProcessEnableAttribtue(processElement, "preparingOperation", configuration.PreparingOperation);
            SetProcessEnableAttribtue(processElement, "sentBeforeAllAction", configuration.SentBeforeAllAction);
            SetProcessEnableAttribtue(processElement, "getRefunds", configuration.GetRefunds);
            SetProcessEnableAttribtue(processElement, "getTrades", configuration.GetTrades);
            SetProcessEnableAttribtue(processElement, "getTradesInfo", configuration.GetTradesInfo);
            SetProcessEnableAttribtue(processElement, "updateTradesInfo", configuration.UpdateTradesInfo);
            SetProcessEnableAttribtue(processElement, "sent", configuration.Sent);
            SetProcessEnableAttribtue(processElement, "tradeNoCreatePay", configuration.TradeNoCreatePay);
            SetProcessEnableAttribtue(processElement, "waitBuyerPay", configuration.WaitBuyerPay);
            SetProcessEnableAttribtue(processElement, "waitSellerSendGoods", configuration.WaitSellerSendGoods);
            SetElementValue<int>(processElement, "processinterval", configuration.ProcessInterval);
            SetElementValue<int>(processElement, "updatebeforedays", configuration.UpdateBeforeDays);
            SetElementValue<bool>(processElement, "updateWhenGetTrade", configuration.UpdateWhenGet);
            SetElementValue<bool>(processElement, "useIncrement", configuration.UseIncrement);
            SetElementValue<bool>(processElement, "updateTradeDetail", configuration.EnableUpdateTradeDetail);
            xDocument.Save("configuration.xml");
        }

    }
    #endregion
}
